package org.cainiao.io.netty.other.callback;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * <br />
 * <p>
 * Author: Cai Niao(wdhlzd@163.com)<br />
 */
public class FutureTest {

    public static void main(String[] args) {
        Channel channel = null;
        // Does not block
        ChannelFuture future = channel.connect(new InetSocketAddress("192.168.0.1", 25));
        future.addListener((ChannelFutureListener) future1 -> {
            if (future1.isSuccess()) {
                ByteBuf buffer = Unpooled.copiedBuffer("Hello", Charset.defaultCharset());
                ChannelFuture wf = future1.channel().writeAndFlush(buffer);
                // ....
            } else {
                Throwable cause = future1.cause();
                cause.printStackTrace();
            }
        });
    }

}
